$(document).ready(function() {
	/*独家分页*/
	MyObjectogHHH = {
		//url : "http://localhost/app/exp/order",
		url:Server + "/app/exp/order",
		haveuid : $("#useridoflogin").val(),
		ishavesend : 0,
		boolean : false,
		type : "POST",
		dataType : "JSON",
		number : 1,
		AllCount : 0,
		nowp : 0,
		Callback : function(nowPage, ishavasend) {
			MyObjectogHHH.nowp = nowPage;
			console.log(MyObjectogHHH.nowp-1, MyObjectogHHH.haveuid, MyObjectogHHH.ishavesend);
			order(MyObjectogHHH.nowp-1, MyObjectogHHH.haveuid, MyObjectogHHH.ishavesend);
		}
	};

	function MyObjectogHHHofpageload() {
		$("#page").initPage((function() {
			$.ajax({
				"url" : MyObjectogHHH.url,
				"type" : MyObjectogHHH.type,
				"dataType" : MyObjectogHHH.dataType,
				"async" : MyObjectogHHH.boolean,
				"data" : {
					"offset" : MyObjectogHHH.number,
					"limit" : MyObjectogHHH.number,
					"uId" : MyObjectogHHH.haveuid,
					"status" : MyObjectogHHH.ishavesend
				},
				success : function(data) {
					MyObjectogHHH.AllCount = data.total;
				}
			});
			return MyObjectogHHH.AllCount;
		})(), MyObjectogHHH.number, MyObjectogHHH.Callback);
	}
	MyObjectogHHHofpageload();
	/*6,7已回复*/
	$("#havesend").click(function() {
		MyObjectogHHH.ishavesend = 1;
		MyObjectogHHHofpageload();
	})
	$("#nothavesend").click(function() {
		MyObjectogHHH.ishavesend = 0;
		MyObjectogHHHofpageload();
	})
})






function order(offset, uId, ishavesend) {
	$.ajax({
		//"url" : "http://localhost/app/exp/order",
		url:Server + "/app/exp/order",
		"type" : "Post",
		"dataType" : "Json",
		"data" : {
			"offset" : offset,
			"limit" : 10,
			"uId" : uId,
			"status" : ishavesend
		},
		success : function(data) {
			console.log(JSON.stringify(data))
			var m = data.rows;
			var html = "";
			var item = "";
			var pattern = '<li >\
							<div class="row1" >\
								<div>\
									<span>#creatTime </span>\
									<span>订单编号: #number</span>\
								</div>\
							    <div><img class="delIcon" src="images/usercenter_icon2.png" /></div>\
							</div>\
							<div class="row2">\
								<div class="tab1"><img src="#6" /></div>\
								<div class="tab2"><a href="productDetail5?id=#8">#name</a></div>\
								<div class="tab3">#model</div>\
								<div class="tab4">#pname</div>\
								<div class="tab5">\
									<p>#1</p>\
									<p>\
										<a onclick="findOrder(#5)">查看详情</a>\
									</p>\
								</div>\
							<div class="tab6">\
								<div class="btn" name="insBtn">\
									<a onclick="Operation(#3,#4,#7)">#2</a>\
								</div>\
							</div>\
						</div>\
							</li>'
			for (var i = 0; i < m.length; i++) {
				//var time = (m[i].createTime).substring(0, (m[0].createTime).indexOf(' '));
				var status = "";
				var status1 = "";
				switch (m[i].status) {
				case 2:
					status = "待实验";
					status1 = "";
					break;
				case 3:
					status = "已实验";
					status1 = "评价";
					break;
				case 4:
					if(m[i].rate == 0 || m[i].rate == 1){
						status = "已实验";
						status1 = "评价";
					}else if(m[i].rate == 2 || m[i].rate == 3){
						status = "我已评价";
						status1 = "";
					}
					break;
				}
				if (m[0].status > 1) {
					item += pattern
						.replace("#creatTime", m[i].createTime)
						.replace("#number", m[i].number)
						.replace("#name", m[i].chineseName)
						.replace("#model", m[i].insInfo.serial)
						.replace("#pname", m[i].realname)
						.replace("#1", status)
						.replace("#2", status1)
						.replace("#3", m[i].id)
						.replace("#4", m[i].status)
						.replace("#5", m[i].id)
						.replace("#6", m[i].insInfo.infoSrc)
						.replace("#7", m[i].rate)
						.replace("#8", m[i].inId)
				}
			}
			$("#orderList").html(item);
			$("div[name='insBtn']").each(function(){
				var btnContent=$(this).children().eq(0).text();
				if(btnContent==""){
					$(this).parent().empty();
				}else if(btnContent=="待实验"||btnContent=="已评价"){
					$(this).next().hide();
				}
			});
			$(".delIcon").each(function(){
				if(ishavesend==0){
					$(this).hide()
				}
				$(this).click(function() {
					var r=confirm("确定要删除吗？");
					if(r==true){
						$.ajax({
							url :Server + "/orderRelevant/deleteOrder",
								//url :"http://localhost/orderRelevant/deleteOrder",
							data : {
								"id":$(this).attr("val")
							},
							type : "POST",
							//dataType: "JSON",
							success : function(data) {
								alert(data);
								window.location.reload();
							},
						});	
					}
				});
			});
		}
	})
}

function findOrder(id) {
	$(location).attr('href', 'OrderDetail25_2?id=' + id + '');
}
function Operation(id3, Status3,rate) {
	if (Status3 == 9) {//暂时没用
		$(location).attr('href', 'new_file25_1?id=' + id3 + '');
	//		$.ajax({
	//			url:"http://127.0.0.1/app/exp/modifyStatus",
	//			type : "POST",
	//			data:{
	//				"id" : id3,
	//				"status" : 4
	//			},
	//			success : function(data){
	//				alert("修改成功！");
	//			},
	//			error : function(msg){
	//				alert("修改失败，请联系管理员！");
	//			}			
	//		})
	} else if (Status3 == 3 || (Status3 == 4 && (rate == 0 || rate == 1))) {
		$(location).attr('href', 'lab_order_details27?id=' + id3 + '');
	}
}